/*
 * JustaDamnTest.c
 *
 * Created: 6/4/2013 6:45:06 PM
 *  Author: Crossfire
 */ 


// Program for Slave mode
#include<avr/io.h>
#include<util/delay.h>

void TWI_init_slave(void);
void TWI_match_read_slave(void);
void TWI_read_slave(void);
void TWI_match_write_slave(void);
void TWI_write_slave(void);

unsigned char Tsplitsing[7];
unsigned char Tsplitsing2[7][3];

unsigned char write_data,recv_data;
unsigned char data = 0;
unsigned char Tjunction_Address = 0x20;

int main(void)
{
	DDRB=0xff;
	DDRA=0x00;
	PORTB = ~0;
	TWI_init_slave(); // Function to initilaize slave
	while(1)
	{
		//Loop tot correcte bevestiging is ontvangen
		while((TWSR & 0xF8)!= 0x60)
		{
			//Ontvang bevestiging, inschakelen van de TWI, Wissen van TWI interrupt flag
			TWCR=(1<<TWEA)|(1<<TWEN)|(1<<TWINT);
			while (!(TWCR & (1<<TWINT)))  // Wait for TWINT flag
			{
				//dostuff while waiting for I2C communication.
				if((PINA & 1) == 0)
				{
					//PORTB = ~Tsplitsing[0];
					PORTB = ~Tsplitsing2[0][0];
				}
				else if((PINA & 2) == 0)
				{
					//PORTB = ~Tsplitsing[6];
					PORTB = ~Tsplitsing2[6][0];
				}
			}
		}
		TWI_read_slave();
		parse_data_tsplitsing(recv_data);
		PORTB = 0;
		_delay_ms(10);
		PORTB = ~0;
	}
}

uint8_t get_data(int state, int traficlight)//een functie om data makkelijk uit de array te halen (state is minimaal 1 en maximaal 7, trficlight is minimaal 1 en maximaal 3)
{
	return ((Tsplitsing[state-1] >> (2*(((traficlight-4)*-1)-1)))&3);
}

void readblah(unsigned char data)
{
	TWI_match_read_slave();
	TWI_read_slave();
	data = recv_data;
}

//Function to initilaize slave
void TWI_init_slave(void)
{
	//Fill slave address to TWAR
	TWAR=Tjunction_Address;
}

// Function to write data
void TWI_write_slave(void)
{
	//Fill TWDR register whith the data to be sent
	TWDR= write_data;
	//Enable TWI, Clear TWI interrupt flag
	TWCR= (1<<TWEN)|(1<<TWINT);
	//Wait for the acknowledgement
	while((TWSR & 0xF8) != 0xC0);
}

void TWI_match_write_slave(void) //Function to match the slave address and slave dirction bit(write)
{
	while((TWSR & 0xF8)!= 0xA8)	// Loop till correct acknoledgement have been received
	{
		// Get acknowlegement, Enable TWI, Clear TWI interrupt flag
		TWCR=(1<<TWEA)|(1<<TWEN)|(1<<TWINT);
		// Wait for TWINT flag
		while (!(TWCR & (1<<TWINT)));
	}
}

void TWI_read_slave(void)
{
	// Clear TWI interrupt flag,Get acknowlegement, Enable TWI
	TWCR= (1<<TWINT)|(1<<TWEA)|(1<<TWEN);
	// Wait for TWINT flag
	while (!(TWCR & (1<<TWINT)));
	// Wait for acknowledgement
	while((TWSR & 0xF8)!=0x80);
	// Get value from TWDR
	recv_data=TWDR;
}

//Function to match the slave address and slave dirction bit(read)
void TWI_match_read_slave(void)
{
	// Loop till correct acknoledgement have been received
	while((TWSR & 0xF8)!= 0x60)
	{
		// Get acknowlegement, Enable TWI, Clear TWI interrupt flag
		TWCR=(1<<TWEA)|(1<<TWEN)|(1<<TWINT);
		// Wait for TWINT flag
		while (!(TWCR & (1<<TWINT)));
	}
}
